<?php
/**
 * NE - Framework. Base controller.
 * 
 * Base controller class.
 * Contains all necessary base method and properties
 * For othe classes. All other Controller classes should
 * Extends this class
 *
 * @author Eugene I. Nezhuta
 * @copyright (c) 2009 - 2010 NeLab <www.ne-lab.com>
 */

abstract class Controller {

    /**
     * View object
     *
     * @var View
     */
	protected $_view = '';

    /**
     * Model object
     *
     * @var Model
     */
	protected $_model = '';

    /**
     * Request object
     *
     * @var HttpRequest
     */
	protected $_request = '';

    /**
     * Session object
     *
     * @var Session
     */
	protected $_session = '';

    /**
     * Controller errors
     *
     * @var array
     */
	protected $_errors = array();

    /**
     * User object
     *
     * @var User
     */
	protected $_currentUser = '';
	
	/**
	 * Constuctor
	 *
	 */
	public function __construct($model) {
        $this->_model = $model;
		$this->_view    = new View;
		$this->_session = Session::getInstance();
		$this->_request = HttpRequest::getInstance();
		$this->_view->setTplDir('templates/');
		$this->_session->start();
		$this->_view->assign('isLogged', $this->_session->isLogged());
		if($this->_session->isLogged()) {
			$this->_currentUser = $this->_session->getCurrent();
			$this->_view->assign('currentUser', $this->_currentUser);
			if($this->_session->getString('websiteName')) {
				$this->_view->assign('websiteName', $this->_session->getString('websiteName'));
			}
			$setting = new UserSettings($this->_currentUser->id);
			$this->_session->set('lang', $setting->getLanguage());
		}
		$this->_view->assign('panel', true);
		$this->_setSysLang();
	}

	protected function _setSysLang() {
		$language = $this->_session->checkSet('lang') ? $this->_session->getString('lang') : Config::LANG_ENG;
		switch ($language) {
			case Config::LANG_ENG :
				require_once Config::$langDir . '/english.php';
			break;
			case Config::LANG_RUS :
				require_once Config::$langDir . '/russian.php';
			break;
		}
		$this->_view->assign('translator', $translator);
	}

    /**
     * If user not logged in system, then redirect to index page
     */
    public function checkLoginRedirect() {
        if(!$this->_session->isLogged()) {
            $this->redirect('index');
        }
    }

	/**
	 * Common action. Must be redeclared in all other controllers
	 *
	 */
	abstract public function indexAction();
	
	
	/**
	 * Add error
	 *
	 * @param string $error
	 * @param string $namespace
	 */
	public function addError($error, $namespace = null) {
		if(!empty($namespace)) {
			$this->_errors[$namespace] = $error;
		}
		else {
			$this->_errors[] = $error;
		}
	}
	
	
	/**
	 * Check if errors present
	 *
	 * @return boolean
	 */
	public function isErrors() {
		return empty($this->_errors) ? 0 : 1;
	}
	
	/**
	 * Get errors
	 *
	 * @param string $namespace
	 * @return array
	 */
	public function getErrors($namespace = null) {
		if($namespace) {
			return $this->_errorsp[$namespace];
		}
		return $this->_errors;
	}

    /**
     * Redirect to controller action
     *
     * If rrule set to true we are using differnt format of
     * redirect link
     *
     * @param string $controllerName
     * @param string $action
     * @param boolean $rrule
     */
	public function redirect($controllerName, $action = 'index', $rrule = false) {
		if($rrule) {
			header("Location: /".$controllerName."/".$action);
		}
		else {
			header("Location: index.php?cont=".$controllerName."&amp;act=".$action);
		}
	}

    /**
     * Call controller actio without redirect
     *
     * @param string $action
     */
	public function delegateToAction($action) {
		$delegateAction = $action.'Action';
		$this->$delegateAction();	
	}

    /**
     * Automatical error assignment
     */
    public function setErrors() {
        if($this->isErrors()) {
            $this->_view->assign('errors', $this->getErrors());
        }
    }
}